package net.socket.test;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @projectName: java-sample
 * @description:
 * @author: Adam
 * @create: 2020-01-06 09:52
 **/

public class Server {
    public static void main(String[] args) throws IOException {
        //创建一个serverSocket，用于监听客户端socket的连接请求
        ServerSocket ss = new ServerSocket(30000);
        //采用循环不断的接收来自客户端的请求
        while (true){
            //每当接收到客户端socket请求时，服务端也对应产生一个socket
            Socket s = ss.accept();
            //将Socket对应的输出流包装成PrintStream
            PrintStream ps = new PrintStream(s.getOutputStream());
            //进行普通IO操作
            ps.println("你好，我是来自服务端的消息");
            //关闭输出流，关闭socket
            ps.close();
            s.close();
        }
    }
}
